#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"

int count = 0;
class TopicSubscriber : public rclcpp::Node
{
public:
    TopicSubscriber(std::string name) : Node(name)
    {
        RCLCPP_INFO(this->get_logger(), "节点 %s 已经启动!", name.c_str());
        subscriber_ = this->create_subscription<std_msgs::msg::String>("/topic_msg", 10,
        std::bind(&TopicSubscriber::topic_callback, this, std::placeholders::_1));
    }

private:
    void topic_callback(const std_msgs::msg::String::SharedPtr msg)
    {
        RCLCPP_INFO(get_logger(), "subscribe: %s", msg->data.c_str());
        
    }
    rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscriber_;
};

int main(int argc, char *argv[])
{
    rclcpp::init(argc, argv);
    auto node = std::make_shared<TopicSubscriber>("topic_subscriber");
    rclcpp::spin(node);
    rclcpp::shutdown();
    return 0;
}
